package com.ym.medical.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.common.enums.UserUserTypeEnum;
import com.ym.common.utils.StringUtil;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.WxUserDao;
import com.ym.medical.domain.entity.WxUserEntity;
import com.ym.medical.domain.param.WxUserQueryParam;
import com.ym.medical.domain.vo.WxUserListPcVO;
import com.ym.medical.enums.PlaformSourceEnum;
import com.ym.medical.service.IAccountSV;
import com.ym.medical.service.IFileInforSV;
import com.ym.medical.service.ISysSignSV;
import com.ym.medical.service.IUserSV;
import com.ym.medical.service.IWxUserSV;
import com.ym.medical.service.IYunxinUserSV;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 微信用户Service业务层处理
 *
 * @author ym
 * @date 2020-08-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WxUserSVImpl extends BaseSVImpl<WxUserEntity, WxUserDao> implements IWxUserSV {

	@Autowired
	private WxMaService wxMaService;

	@Autowired
	@Lazy
	private IUserSV userSV;

	@Autowired
	@Lazy
	private IYunxinUserSV yunxinUserSV;

	@Autowired
	private IAccountSV accountSV;

	@Autowired
	private ISysSignSV sysSignSV;

	@Autowired
	private IFileInforSV fileInforSV;

	@Autowired
	private ConfigCenterProperties configCenterProperties;

	/**
	 * 查询微信用户列表
	 *
	 * @param queryParam 微信用户 查询参数
	 * @return 微信用户
	 */
	@Override
	public List<WxUserListPcVO> selectListPC(WxUserQueryParam queryParam) {
		List<WxUserListPcVO> wxUsers = mapper.selectListPC(queryParam);
		return wxUsers;
	}


	@Override
	public WxUserEntity createWxUserMa(Long userId, UserUserTypeEnum userTypeEnum, WxMaUserInfo wxMaUserInfo) {
		WxUserEntity wxUserEntity = new WxUserEntity();
		wxUserEntity.setUnionid(wxMaUserInfo.getUnionId());
		wxUserEntity.setOpenidMini(wxMaUserInfo.getOpenId());
		wxUserEntity.setUserId(userId);
		wxUserEntity.setIsAuthPhone(FlagEnum.NO.getKey());
		wxUserEntity.setUserType(userTypeEnum.getKey());
		wxUserEntity.setAgreeProtoFlag(FlagEnum.NO.getKey());
		this.insert(wxUserEntity);
		return wxUserEntity;
	}

	@Override
	public WxUserEntity createWxUserMp(Long userId, UserUserTypeEnum userTypeEnum, WxMpUser wxMpUser) {
		WxUserEntity wxUserEntity = new WxUserEntity();
		wxUserEntity.setUnionid(wxMpUser.getUnionId());
		wxUserEntity.setUserId(userId);
		wxUserEntity.setOpenidMini(wxMpUser.getOpenId());
		wxUserEntity.setCity(wxMpUser.getCity());
		wxUserEntity.setCountry(wxMpUser.getCountry());
		if (wxMpUser.getSex() != null) {
			wxUserEntity.setGender(wxMpUser.getSex().toString());
		}
		wxUserEntity.setIsAuthPhone(FlagEnum.NO.getKey());
		wxUserEntity.setLanguage(wxMpUser.getLanguage());
		wxUserEntity.setProvince(wxMpUser.getProvince());
		wxUserEntity.setWxTxUrl(wxMpUser.getHeadImgUrl());
		wxUserEntity.setUserType(userTypeEnum.getKey());
		wxUserEntity.setWxUserNc(wxMpUser.getNickname());
		// 昵称不为空,表示授权
		if (StringUtil.isNotBlank(wxUserEntity.getWxUserNc())) {
			wxUserEntity.setAgreeProtoFlag(FlagEnum.IS.getKey());
			wxUserEntity.setInfoTime(new Date());
		} else {
			wxUserEntity.setAgreeProtoFlag(FlagEnum.NO.getKey());
		}
		this.insert(wxUserEntity);
		return wxUserEntity;
	}


	@Override
	public String getOpenId(Long toUser, PlaformSourceEnum plaformSourceEnum) {
		if (toUser == null || plaformSourceEnum == null) {
			return null;
		}
		WxUserEntity wxUser = selectById(toUser);
		String openId = null;
		switch (plaformSourceEnum) {
			case THE_PUBLIC:
				openId = wxUser.getOpenidPlat();
				break;
			case SMALL_PROGRAM:
				openId = wxUser.getOpenidMini();
				break;
		}
		return openId;
	}


	@Override
	public WxUserEntity selectByUserId(Long userId) {
		return this.selectOne(
				SelectWhere()
						.andEqualTo(WxUserEntity::getUserId, userId)
						.end()
		);
	}

	@Override
	public WxUserEntity synchronizeWeChatDataMa(Long userId, WxMaUserInfo userInfo) {
		WxUserEntity wxUser = this.selectByUserId(userId);
		if (FlagEnum.NO.getKey().equals(wxUser.getAgreeProtoFlag())) {
			// 1.表示该用户未授权，开始授权
			wxUser.setWxTxUrl(userInfo.getAvatarUrl());
			wxUser.setWxUserNc(userInfo.getNickName());
			wxUser.setCity(userInfo.getCity());
			wxUser.setGender(userInfo.getGender());
			wxUser.setLanguage(userInfo.getLanguage());
			wxUser.setCountry(userInfo.getCountry());
			wxUser.setProvince(userInfo.getProvince());
			wxUser.setUnionid(userInfo.getUnionId());
			wxUser.setAgreeProtoFlag(FlagEnum.IS.getKey());
			this.updateById(wxUser);
		}
		return wxUser;
	}
}
